package net.huashitong.supervision.task.controller;

import ch.qos.logback.core.filter.EvaluatorFilter;
import com.sirdc.modules.core.web.JsonBaseController;
import com.sirdc.modules.core.web.model.JqGrid;
import com.sirdc.modules.core.web.model.Message;
import com.sirdc.modules.sys.databean.SysUserDataBean;
import com.sirdc.modules.sys.entity.SysDept;
import com.sirdc.modules.sys.util.SysUserUtils;
import com.sirdc.modules.utils.StringUtils;
import net.huashitong.supervision.task.entity.EvalRules;
import net.huashitong.supervision.task.entity.TaskMain;
import net.huashitong.supervision.task.filter.EvalRulesFilter;
import net.huashitong.supervision.task.filter.TaskMainFilter;
import net.huashitong.supervision.task.service.EvalRuleService;
import net.huashitong.supervision.task.service.TaskMainService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: ling
 * Date: 2019/11/11
 * Time: 16:14
 * To change this template use File | Settings | File Templates.
 * Description:管理员菜单
 */
@Controller
@RequestMapping(value = "/admin")
public class AdminController extends JsonBaseController {
    @Autowired
        private TaskMainService taskMainService;

    @Autowired
    private EvalRuleService ruleService;
    @Override
    protected String getView(String s) {
        return "/modules/task/admin/"+s;
    }

    /**
     * 全部任务
     * @return
     */
    @RequestMapping(value = "/allTask")
    public String allTask(){
        return getView("/allTask");
    }

    @ResponseBody
    @RequestMapping(value = "allTask",method = RequestMethod.POST)
    public JqGrid allTask(TaskMainFilter filter){
        List<TaskMain> taskMainList = taskMainService.queryFilter(filter);
        return coverJqGrid(filter,taskMainList );
    }
    //设置评价比例
    @RequestMapping(value = "/evalRules")
    public String showRules(){
        return getView("/ruleQuery");
    }

    @ResponseBody
    @RequestMapping(value = "/evalRules",method = RequestMethod.POST)
    public JqGrid getRules(EvalRulesFilter filter){

        if(StringUtils.isBlank(filter.getRuleId()))
        {
            filter.setRuleId("1");
        }

        List<EvalRules> evalRules = ruleService.getEvalRules(filter);

        return coverJqGrid(filter,evalRules );
    }


    //编辑占比
    @RequestMapping(value = "/evalRules/edit/{id}", method = RequestMethod.GET)
    public String showEditView(Model model, @PathVariable String id) {
        //根据自己的过滤规则过滤部门，将来可以考虑是否要修改成下拉树

        EvalRules evalRules = ruleService.getById(id);
        model.addAttribute("evalRules", evalRules);
        return getView("form");
    }

    @ResponseBody
    @RequestMapping(value = "/evalRules/edit", method = RequestMethod.POST)
    public Message update(@ModelAttribute EvalRulesFilter filter) {
        String sysId = filter.getSysId();
        if (StringUtils.isBlank(sysId)||StringUtils.isBlank(filter.getRuleRatio())) {
            return  coverMessage("200");
        }
        EvalRules entity = ruleService.getById(sysId);
        entity.setRuleRatio(filter.getRuleRatio());
        ruleService.update(entity);
        return coverMessage("200");

    }

}
